﻿using System;
using System.Linq.Expressions;

using VirtualTournaments.Business.Players;
using VirtualTournaments.DataAccess.SqlServer.DataMappers;

namespace VirtualTournaments.DataAccess.SqlServer.DataProxies
{
	internal sealed class PlayerDataProxy : Business.Players.Player, IDataProxy<SqlServer.Player>, Identifiable<SqlServer.Player>
	{
		internal PlayerDataProxy(SqlServer.Player dataObject)
			: base(new Name(dataObject.FirstName, dataObject.Surname), null, (Roles)dataObject.Role, Convert.ToByte(dataObject.Speed), Convert.ToByte(dataObject.Precision), Convert.ToByte(dataObject.Strength), Convert.ToByte(dataObject.Dribbling))
		{
			InnerDataObject = dataObject;
			MapProperties();
		}

		#region Override properties

		internal bool TeamHasBeenRead { get; set; }
		public override Business.Tournaments.Team Team
		{
			get
			{
				if (!TeamHasBeenRead)
				{
					TeamHasBeenRead = true;
					if (base.Team == null)
					{
						base.Team = MapTeam();
					}
				}
				return base.Team;
			}
		}

		#endregion

		#region Mapping methods

		private void MapProperties()
		{
		}

		private Business.Tournaments.Team MapTeam()
		{
			return TeamsDataMapper.Instance.DataObject2BusinessObject(InnerDataObject.Team);
		}

		#endregion

		#region IDataProxy<Player> Members

		public SqlServer.Player InnerDataObject { get; set; }

		#endregion

		#region Identifiable<Player> Members

		public Expression<Func<Player, bool>> GetUniqueWhereExpression()
		{
			throw new NotImplementedException();
		}

		#endregion
	}
}
